package cnki.util;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Objects;

/**
 * @author: Han
 * @date: 2018/9/29 10:18
 * @dscription: TODO
 */
public class FileManager {
    private Class aClass;

    public FileManager(Class aClass) {
        this.aClass = aClass;
    }

    /**
     * 指定是否追加到文件尾部字符串
     *
     * @param str      待写入字符串
     * @param isAppend 是否追加
     */
    public synchronized void write(String str, boolean isAppend) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                    "F:\\log\\" + aClass.getName() + ".log", isAppend)));
            writer.write(str);
            writer.newLine();
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(writer).close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
